package com.mou.controller.user;

import com.mou.annotation.Log;
import com.mou.constant.JwtClaimsConstant;
import com.mou.context.BaseContext;
import com.mou.dto.PayPasswordDTO;
import com.mou.dto.UserDTO;
import com.mou.dto.UserLoginDTO;
import com.mou.entity.OnlineUser;
import com.mou.entity.User;
import com.mou.properties.JwtProperties;
import com.mou.result.Result;
import com.mou.service.CaptchaService;
import com.mou.service.OnlineUserService;
import com.mou.service.UserService;
import com.mou.utils.JwtUtil;
import com.mou.vo.UserLoginVO;
import com.mou.vo.UserProfileVO;
import com.mou.vo.UserVO;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Slf4j
@RequestMapping("/user/user")
@RestController("userUserController")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private OnlineUserService onlineUserService;
    @Autowired
    private CaptchaService captchaService;
    @Autowired
    private JwtProperties jwtProperties;

    @PostMapping("/login")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO) {
        log.info("user:{}", userLoginDTO);
        User user = userService.userLogin(userLoginDTO);
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID, user.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getSecretKey(),
                jwtProperties.getTtl(),
                claims);
        OnlineUser onlineUser = OnlineUser.builder()
                .userId(user.getId())
                .token(token)
                .build();
        onlineUserService.updateOnlineUser(onlineUser);

        UserLoginVO userLoginVO = UserLoginVO.builder()
                .id(user.getId())
                .nickname(user.getNickname())
                .roleId(user.getRoleId())
                .avatar(user.getAvatar())
                .token(token)
                .build();
        return Result.success(userLoginVO);
    }

    @Log
    @PostMapping("/register")
    public Result<String> register(@RequestBody @Valid UserDTO userDTO) {
        if (!captchaService.validateCode(userDTO.getEmail(), userDTO.getCaptcha())) {
            return Result.error("验证码错误或已过期");
        }
        log.info("注册用户:{}", userDTO);
        userService.add(userDTO);
        return Result.success();
    }

    @Log
    @PutMapping("/forgotPassword")
    public Result<String> forgotPassword(@RequestBody @Valid UserDTO userDTO) {
        if (!captchaService.validateCode(userDTO.getEmail(), userDTO.getCaptcha())) {
            return Result.error("验证码错误或已过期");
        }
        log.info("忘记密码:{}", userDTO);
        userService.forgotPassword(userDTO);
        return Result.success();
    }

    @PostMapping("/profile/{id}")
    public Result<UserProfileVO> profile(@PathVariable("id") Long id) {
        log.info("查询用户:{}", id);
        UserProfileVO userProfileVO = userService.getProfileById(id);
        return Result.success(userProfileVO);
    }

    @PutMapping("/updateProfile")
    public Result<String> updateProfile(@RequestBody @Validated UserDTO userDTO) {
        log.info("修改用户:{}", userDTO);
        User user = new User();
        BeanUtils.copyProperties(userDTO, user);
        userService.updateProfile(user);
        return Result.success();
    }

    @PutMapping("/updatePayPassword")
    public Result<String> updatePayPassword(@RequestBody @Validated PayPasswordDTO payPasswordDTO) {
        log.info("修改支付密码:{}", payPasswordDTO);
        if(Objects.equals(payPasswordDTO.getEmail(), "")){
            return Result.error("邮箱不能为空");
        }
        if (!captchaService.validateCode(payPasswordDTO.getEmail(), payPasswordDTO.getCaptcha())) {
            return Result.error("验证码错误或已过期");
        }
        userService.updatePayPassword(payPasswordDTO);
        return Result.success();
    }


}
